package com.jiang.smartcommunity.ui.activity;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import androidx.cardview.widget.CardView;
import androidx.constraintlayout.utils.widget.ImageFilterView;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.jiang.smartcommunity.R;
import com.jiang.smartcommunity.tool.MyAdapter;
import com.jiang.smartcommunity.tool.Tool;

import java.io.FileNotFoundException;
import java.util.List;

public class PicActivity extends BaseActivity implements View.OnClickListener {

    private ImageFilterView imageView7;
    private ImageView imageView3;
    private CardView cv;
    private TextView textView3;
    private TextView tv_name;
    private TextView textView9;
    private TextView tv_none;
    private RecyclerView rv;
    private EditText editTextTextPersonName2;
    private Button button3;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_pic);
        initView();
        setTitle("图文状态");

        S3Activity.Bean bean = Tool.beans.get(Tool.carIndex);
        if (bean.bitmap != null) {
            imageView3.setImageBitmap(bean.bitmap);

        } else {
            imageView3.setImageResource(bean.res);
        }
        imageView7.setImageResource(bean.ava);
        tv_name.setText(bean.name);
        textView3.setText(bean.c);
        rv.setLayoutManager(new LinearLayoutManager(getThis()));

        refreshUI();
    }

    private void refreshUI() {
        List<S3Activity.Bean.LiuYan> liuYans = Tool.beans.get(Tool.carIndex).liuYans;
        tv_none.setVisibility((liuYans != null && !liuYans.isEmpty()) ? View.GONE : View.VISIBLE);
        rv.setAdapter(new MyAdapter<S3Activity.Bean.LiuYan>(getThis(), R.layout.item_comm, liuYans) {
            private ImageFilterView imageView10;
            private TextView textView38;
            private TextView textView39;

            @Override
            public void init(View root, int position) {
                imageView10 = (ImageFilterView) root.findViewById(R.id.imageView10);
                textView38 = (TextView) root.findViewById(R.id.textView38);
                textView39 = (TextView) root.findViewById(R.id.textView39);

                S3Activity.Bean.LiuYan item = getItem(position);
                imageView10.setImageResource(item.ava);
                textView38.setText(item.name);
                textView39.setText(item.c);
            }
        });
    }

    private void initView() {
        imageView7 = (ImageFilterView) findViewById(R.id.imageView7);
        imageView3 = (ImageView) findViewById(R.id.imageView3);
        cv = (CardView) findViewById(R.id.cv);
        textView3 = (TextView) findViewById(R.id.textView3);
        tv_name = (TextView) findViewById(R.id.tv_name);
        textView9 = (TextView) findViewById(R.id.textView9);
        tv_none = (TextView) findViewById(R.id.tv_none);
        rv = (RecyclerView) findViewById(R.id.rv);
        editTextTextPersonName2 = (EditText) findViewById(R.id.editTextTextPersonName2);
        button3 = (Button) findViewById(R.id.button3);

        button3.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.button3:
                submit();
                break;
        }
    }

    private void submit() {
        // validate
        String editTextTextPersonName2String = editTextTextPersonName2.getText().toString().trim();
        if (TextUtils.isEmpty(editTextTextPersonName2String)) {
            Toast.makeText(this, "留言不能为空", Toast.LENGTH_SHORT).show();
            return;
        }

        toast("留言成功");
        Tool.beans.get(Tool.carIndex).liuYans.add(
                new S3Activity.Bean.LiuYan(editTextTextPersonName2String, "住户301", R.drawable.tx3));
        refreshUI();
        editTextTextPersonName2.setText("");
    }
}